package com.jwdf.uda.view.dialog;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.DialogUploadBinding;
import com.jwdf.uda.event.UploadProgressEvent;

/**
 * Created by Administrator on 2016/7/28.
 */
public class UploadProgressDialog extends BaseDialog {
    private DialogUploadBinding mDataBinding;
    private boolean mCancelable = true;
    private AnimationDrawable mFrameAnim;

    public UploadProgressDialog(Context context) {
        super(context);
    }

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mDataBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_upload, null, false);
        return mDataBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        mFrameAnim = (AnimationDrawable) mDataBinding.ivLeafAnim.getBackground();
    }

    private void setProgress(int progress) {
        if (!mFrameAnim.isRunning()) {
            mFrameAnim.start();
        }
        mDataBinding.progressBar.setProgress(progress);
    }

    public void setCanDissmiss(boolean cancelable) {
        this.mCancelable = cancelable;
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
        }
    }

    public void onEventMainThread(UploadProgressEvent event) {
        setProgress(event.progress);
    }
}
